package com.vitabuybuy.tv.ui.mine.talent

import android.os.Bundle
import android.view.View
import com.library.common.util.FragmentUtils
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityExpertZoneBinding
import com.library.common.base.BaseActivity
import com.library.common.base.appContext
import com.library.common.ext.*
import com.library.common.toast.XToast
import com.library.common.util.MMKVContancts
import com.vitabuybuy.tv.BuildConfig
import com.vitabuybuy.tv.ui.mine.talent.live.LiveManageFragment
import com.vitabuybuy.tv.ui.mine.talent.live.StartLiveActivity
import com.vitabuybuy.tv.ui.mine.talent.notice.NoticeFragment
import com.vitabuybuy.tv.ui.mine.talent.task.TaskFragment
import com.vitabuybuy.tv.ui.mine.talent.usercenter.UserCenterFragment
import com.vitabuybuy.tv.ui.mine.talent.viewmodel.TalentApplyViewModel
import com.vitabuybuy.tv.ui.tencent.liveroom.IMLVBLiveRoomListener
import com.vitabuybuy.tv.ui.tencent.liveroom.MLVBLiveRoom
import com.vitabuybuy.tv.ui.tencent.liveroom.roomutil.commondef.LoginInfo
import kotlinx.android.synthetic.main.activity_expert_zone.*

class ExpertZoneActivity : BaseActivity<TalentApplyViewModel, ActivityExpertZoneBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_expert_zone

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.expert_zone))
        mIvBack.visibility = View.GONE
        showCurrentFragment(0)
    }

    override fun eventHandling() {
        navView.setOnNavigationItemSelectedListener { menuItem ->
            mTvRight.visibility = View.GONE
            when (menuItem.itemId) {
                R.id.navigationTask -> {
                    showCurrentFragment(0)
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.navigationNotice -> {
                    showCurrentFragment(1)
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.navigationLive -> {
                    mTvRight.apply {
                        text = getString(R.string.launch_live)
                        visibility = View.VISIBLE
                        setOnClickListener {
                            StartLiveActivity.openActivity(mContext)
                        }
                    }
                    showCurrentFragment(2)
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.navigationPersonalCenter -> {
                    mTvRight.apply {
                        text = getString(R.string.exit)
                        visibility = View.VISIBLE
                        setOnClickListener {
                            onBackPressed()
                        }
                    }
                    showCurrentFragment(3)
                    return@setOnNavigationItemSelectedListener true
                }
            }
            false
        }
    }

    private fun showCurrentFragment(index: Int) {
        when (index) {
            0 -> FragmentUtils.replaceFragment(
                supportFragmentManager,
                R.id.containerFragment,
                TaskFragment::class.java.name
            )
            1 -> FragmentUtils.replaceFragment(
                supportFragmentManager,
                R.id.containerFragment,
                NoticeFragment::class.java.name
            )
            2 -> FragmentUtils.replaceFragment(
                supportFragmentManager,
                R.id.containerFragment,
                LiveManageFragment::class.java.name
            )
            3 -> FragmentUtils.replaceFragment(
                supportFragmentManager,
                R.id.containerFragment,
                UserCenterFragment::class.java.name
            )
        }
    }

    override fun onBackPressed() {
//        MLVBLiveRoom.sharedInstance(appContext).logout{
        val loginInfo = LoginInfo().apply {
            sdkAppID = BuildConfig.LIVEAPPID.toLong()
            userID = mmkv.decodeInt(MMKVContancts.USER_ID, 0).toString()
            userSig = mmkv.decodeString(MMKVContancts.USER_SIGN, "")
            userName = mmkv.decodeString(MMKVContancts.USER_NICKNAME, "")
            userAvatar = mmkv.decodeString(MMKVContancts.USER_AVATAR, "")
        }
        showLoadingExt()
        MLVBLiveRoom.sharedInstance(appContext)
            .login(loginInfo, object : IMLVBLiveRoomListener.LoginCallback {
                override fun onSuccess() {
                    dismissLoadingExt()
                    finish()
                }
                override fun onError(errCode: Int, errInfo: String?) {
                    dismissLoadingExt()
                    errInfo?.run {
                        errCode.logE("JIA--errCode--")
                        this.logE("JIA--errInfo--")
                        XToast.error("直播登录失败，请联系管理员")
                    }
                }
            })
//        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, ExpertZoneActivity::class.java)
        }
    }
}
